package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.SpuPull;
import com.qingcheng.service.goods.SpuService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author shenwei
 *商品下架 删除商品静态页面
 */
@Service
public class SpuPullImpl implements SpuPull {

    @Value("${pagePath}")
    private String pagePath;

    @Reference
    private SpuService spuService;

    public void pull (String spuId) {
        //1.查询商品信息
        Goods goods = spuService.findGoodsById(spuId);
        // 获取spu信息
        Spu spu = goods.getSpu();
        // 获取sku列表
        List<Sku> skuList = goods.getSkuList();

        for (Sku sku : skuList) {
            ///sku.setStatus("0");
            File dest = new File(pagePath + sku.getId() + ".html");
            if(dest.exists()){
                dest.delete();
                //更改数据库状态
                spuService.pull(spuId);
            }
        }
    }
}
